<head>
<meta content="text/html; charset=utf-8" http-equiv="Content-Type" />
<title>System ankiet internetowcyh</title>
</head>

<body>
<div style="text-align:center; width:1024px; height:250px; text-align:center; margin-left:auto; margin-right:auto">
<img src="Logo puste.png"/>
<hr/>
</div>
<div style="text-align:center; width:1024px; text-align:center; margin-left:auto; margin-right:auto">

	<?php
	session_start();
	$link = mysql_connect('localhost', 'root', '')
    	or die('Could not connect: ' . mysql_error());
	echo "<br>";
	mysql_select_db('ankiety') or die('Could not select database');
		
	if(!isset($_GET['pyt']))
	{
		$query = "SELECT ID_QUESTION, QUESTION FROM questions ";
		$result = mysql_query($query) or die('Query failed: ' . mysql_error());

		echo "<h2>Wybierz pytanie</h2>";
		echo "<p style='font-size:x-large'> Witamy w systemie ankiet internetowych, aby wziąć udział w ankiecie wybierz pytanie z listy:</p>";
		echo "<div id=pytania style='text-align:left;'>";
		//echo '<form name="wybierz_pytanie" method="get" action="wczytywanie.php">';
		while ($row = mysql_fetch_row($result)) 
		{
			//echo "<input type='radio' name='pyt' value=$row[0] /> $row[1] <br>";
			echo "<a href='wczytywanie.php?pyt=".$row[0]."'> $row[1] </a><br>";
		}
		//echo "<p><input name='wybor' type='submit' value='Wybieram' /> </p>";
		//echo "</form>";
		echo "<hr/>";
		echo "</div>";
	}
	if(isset($_GET['pyt']))
	{
		$pytanie = $_GET['pyt'];
		$_SESSION['pytanie'] = $_GET['pyt'];
		
		$query = "SELECT MULTIPLE_CHOOSE FROM questions where ID_QUESTION = ".$pytanie."";
		$result = mysql_query($query) or die ('Query failed: ' . mysql_error());
		$row = mysql_fetch_row($result);
		echo "<div id=odp style='text-align:left;'>";
		if($row[0] == 0)
		{				
		$query = "SELECT QUESTION FROM questions where ID_QUESTION = ".$pytanie."";
		$result = mysql_query($query) or die ('Query failed: ' . mysql_error());
		$row = mysql_fetch_row($result);
		echo "<hr />";
		echo "<h2> $row[0] </h2><br>";

		$query = "SELECT ID_ANSWER, ANSWER FROM answers where ID_QUESTION = ".$pytanie."";
		$result = mysql_query($query) or die('Query failed: ' . mysql_error());
		echo '<form name="wybierz_odpowiedz" method="get" action="wczytywanie.php">';
		while ($row = mysql_fetch_row($result)) {
			echo "<input type='radio' name='odp[0]' value=$row[0] /> $row[1] <br>";
			}
		echo "<p><input name='odpowiedz' type='submit' value='Zapisz' /> </p>";
		echo "</form>";
		
		}
		else
		{
		$query = "SELECT QUESTION FROM questions where ID_QUESTION = ".$pytanie."";
		$result = mysql_query($query) or die ('Query failed: ' . mysql_error());
		$row = mysql_fetch_row($result);
		echo "<h2>$row[0] </h2><br>";
		
		$query = "SELECT ID_ANSWER, ANSWER FROM answers where ID_QUESTION = ".$pytanie."";
		$result = mysql_query($query) or die('Query failed: ' . mysql_error());
		echo '<form name="wybierz_odpowiedz" method="get" action="wczytywanie.php">';
		while ($row = mysql_fetch_row($result)) {
			echo "<input type='checkbox' name='odp[]' value=$row[0] /> $row[1] <br>";
			}
		echo "<p><input name='odpowiedz' type='submit' value='Zapisz' /> </p>";
		echo "</form>";
		}
		echo "</div>";
	}
	if(isset($_GET['odp'][0]))
	{
	$licznik = 0;
	while($licznik < sizeof($_GET['odp']))
	{
		$query = "SELECT ANSWERS_COUNT from answers where ID_ANSWER = ".$_GET['odp'][$licznik]."";
		$result = mysql_query($query) or die('Query failed: ' . mysql_error());
		
		$row = mysql_fetch_row($result);
		
		$suma = $row[0] + 1;
		
		$con = mysqli_connect('localhost', 'root', '', 'ankiety');
		$sql = "UPDATE answers SET ANSWERS_COUNT = ".$suma." where ID_ANSWER = ".$_GET['odp'][$licznik]."";
		
		if (!mysqli_query($con,$sql))
			{
			  die('Error: ' . mysqli_error($con));
			}
		$licznik = $licznik + 1;
	}
	echo "<p style='text-align:center'><h1>DZIEKUJEMY ZA UDZIAL W ANKIECIE</h1></p>";
	echo "<form name=wyniki method='post' action='wczytywanie.php'>";
	echo "<p><input name='wysw_wyniki' type='submit' value='Wyswietl wyniki' /> </p>";
	}
	if(isset($_POST['wysw_wyniki']))
	{
	$query = "SELECT QUESTION FROM questions where id_question = ".$_SESSION['pytanie']." ";
	$result = mysql_query($query) or die('Query failed: ' . mysql_error());
	$row = mysql_fetch_row($result);
	echo "<h1>".$row[0]."</h1>";
	$suma = 0;
	$licznik = 0;
	$licznik2 = 0;
	$query = "SELECT ANSWERS_COUNT FROM answers where id_question = ".$_SESSION['pytanie']." ";
	$result = mysql_query($query) or die('Query failed: ' . mysql_error());
	while ($row = mysql_fetch_row($result)) 
	{
	$suma = $suma + $row[0];
	$licznik = $licznik + 1;
	$licznik2 = $licznik2 + 1;
	}
	echo '<img src="http://chart.apis.google.com/chart?chs=500x200&chd=t:';
	$query = "SELECT ANSWERS_COUNT FROM answers where id_question = ".$_SESSION['pytanie']." ";
	$result = mysql_query($query) or die('Query failed: ' . mysql_error());
	while ($row = mysql_fetch_row($result)) 
	{
	$licznik = $licznik - 1;
	$wynik_odp = 0;
	$wynik_odp = $row[0] * 100 / $suma;
	echo $wynik_odp;
	if($licznik > 0)
	{
		echo ",";
	}
	}
	echo "&cht=p3&chl=";
	$query = "SELECT ANSWER FROM answers where id_question = ".$_SESSION['pytanie']." ";
	$result = mysql_query($query) or die('Query failed: ' . mysql_error());
	while ($row = mysql_fetch_row($result)) 
	{
	$licznik2 = $licznik2 - 1;
	
	echo $row[0];
	if($licznik2 > 0)
	{
		echo "|";
	}
	}
	
	
	echo '" alt="Wykres" border="0">';
	}
	
	
	?>
	
	
</div>	
	
</body>